package com.oracle.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oracle.mapper.PublicityMapper;
import com.oracle.pojo.Publicity;
import com.oracle.service.PublicityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PublicityServiceImpl implements PublicityService {

    @Autowired
    private PublicityMapper publicityMapper;

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public IPage<Publicity> findPublicity(Publicity publicity, Integer pageNum, Integer pageSize) {
        IPage<Publicity> page = new Page<>(pageNum, pageSize, true);
        QueryWrapper<Publicity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("tip_Time");
        if (ObjectUtils.isEmpty(publicity.getTipTime())){
            IPage<Publicity> publicityIPage = publicityMapper.selectPage(page, queryWrapper);
            return publicityIPage;
        }else {
            Date tipTime = publicity.getTipTime();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String formattedRecordTime = formatter.format(tipTime);
            queryWrapper.like("tip_Time",formattedRecordTime);
            IPage<Publicity> publicityIPage = publicityMapper.selectPage(page, queryWrapper);
            System.out.println(publicityIPage);
            return publicityIPage;
        }
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public Publicity findOnePublicity(Integer id) {
        QueryWrapper<Publicity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tip_Id",id);
        Publicity publicity = publicityMapper.selectOne(queryWrapper);
        return publicity;
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public Publicity findOnePublicityByState(Integer state) {
        Publicity earliestByStatus = publicityMapper.findEarliestByStatus(state);
        return earliestByStatus;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int insertPublicity(Publicity publicity) {
        int insert = publicityMapper.insert(publicity);
        return insert;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int updatePublicity(Publicity publicity) {
        int i = publicityMapper.updateById(publicity);
        return i;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int deletePublicity(List<Integer> tipId) {
        int i = publicityMapper.deleteBatchIds(tipId);
        return i;
    }

}
